package com.example.hotel_manage.web.controller.position;

import com.example.common.constant.ErrorCode;
import com.example.common.exception.BusinessException;
import com.example.common.pojo.qo.OptionQO;
import com.example.common.pojo.vo.OptionVO;
import com.example.common.pojo.vo.PageVO;
import com.example.hotel_manage.pojo.dto.position.PositionAddDTO;
import com.example.hotel_manage.pojo.dto.position.PositionUpdateDTO;
import com.example.hotel_manage.pojo.mapper.position.PositionMapper;
import com.example.hotel_manage.pojo.po.position.PositionPO;
import com.example.hotel_manage.pojo.qo.position.PositionQO;
import com.example.hotel_manage.pojo.vo.position.PositionListVO;
import com.example.hotel_manage.pojo.vo.position.PositionShowVO;
import com.example.hotel_manage.service.position.PositionService;
import com.example.hotel_manage.web.AbstractController;
import com.example.hotel_manage.web.api.position.PositionAPI;
import com.example.hotel_manage.web.constant.WebConst;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;

/**
 * 【职位】控制器
 */
@RestController
@RequestMapping(WebConst.ModulePath.POSITION + "/position")
public class PositionController extends AbstractController implements PositionAPI {

    @Autowired
    private PositionService positionService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity<PositionShowVO> save(@Valid @RequestBody PositionAddDTO positionAddDTO) throws Exception {
        PositionPO position = positionService.save(positionAddDTO);
        return ResponseEntity.created(new URI(WebConst.ModulePath.POSITION + "/position/" + position.getId()))
                .body(PositionMapper.INSTANCE.toShowVO(position));
    }

    @Override
    @PutMapping
    public ResponseEntity<PositionShowVO> update(@Valid @RequestBody PositionUpdateDTO positionUpdateDTO) {
        PositionPO position = positionService.update(positionUpdateDTO);
        return ResponseEntity.ok(PositionMapper.INSTANCE.toShowVO(position));
    }

    @Override
    @GetMapping
    public ResponseEntity<PageVO<PositionListVO>> list(@Valid PositionQO positionQO) {
        PageVO<PositionListVO> page = positionService.list(positionQO);
        return ResponseEntity.ok(page);
    }

    @Override
    @GetMapping(value = "/options")
    public ResponseEntity<List<OptionVO<Integer, String>>> findOptions(OptionQO<Integer, String> qo) {
        List<OptionVO<Integer, String>> options = positionService.findOptions(qo);
        return ResponseEntity.ok(options);
    }

    @Override
    @GetMapping(value = "/{id}")
    public ResponseEntity<PositionShowVO> show(@PathVariable Integer id) {
        PositionShowVO positionShowVO = positionService.show(id);
        return ResponseEntity.ok(positionShowVO);
    }

    @Override
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<Integer> delete(@PathVariable Integer id) {
        int count = positionService.delete(id);
        return ResponseEntity.ok(count);
    }

    @Override
    @DeleteMapping
    public ResponseEntity<Integer> deleteBatch(@RequestBody Integer[] id) {
        if (ArrayUtils.isEmpty(id)) {
            throw new BusinessException(ErrorCode.PARAM_IS_NULL);
        }
        int count = positionService.delete(id);
        return ResponseEntity.ok(count);
    }

}


